const app = getApp();
const db = wx.cloud.database();
const _ = db.command
Page({
  data: {
    showShare: false,
    ischecked: false,
    userinfo: {},
  },


  onLoad() {
    let ischecked = wx.getStorageSync('ischecked') || false
    let userinfo = wx.getStorageSync('userinfo')
    app.globalData.userInfo = userinfo
    app.globalData.ischecked = ischecked
    if (ischecked) {
      this.setData({
        ischecked: true,
        userinfo
      })
    } else if (!ischecked) {
      this.setData({
        ischecked: false,
        userinfo: getApp().globalData.userInfo,
      })
    }
    wx.setStorageSync('app.globalData.userInfo', userinfo)
    wx.setStorageSync('app.globalData.ischecked', ischecked)
  },

  go(e) {
    let that = this;
    if (e.currentTarget.dataset.status == '2') {
      if (!app.globalData.userInfo) {
        wx.getUserProfile({
          desc: '获取用户信息',
          success(res) {
            let userinfo = res.userInfo
            console.log(res)
            that.setData({
              ischecked: true,
              userinfo
            })
            app.globalData.userInfo = userinfo
            app.globalData.ischecked = !app.globalData.ischecked
            wx.setStorageSync('ischecked', true),
              wx.setStorageSync('userinfo', userinfo),
              wx.setStorageSync('app.globalData.userInfo', userinfo),
              wx.setStorageSync('app.globalData.ischecked', true),
              wx.login({
                //获取code
                success: function (res) {
                  var code = res.code; //返回code
                  console.log(code);
                  var appId = 'wxeaa417fcf3731882'; //微信小程序AppID
                  var secret = 'cd6531120505b900cd8af0dfd41c69e1'; //可在微信公众平台设置扫描二维码获取
                  wx.request({
                    url: `https://api.weixin.qq.com/sns/jscode2session?appid=wxeaa417fcf3731882&secret=cd6531120505b900cd8af0dfd41c69e1&js_code=`+code+`&grant_type=authorization_code`,
                    success:(res)=> {
                      var openid = res.data.openid //返回openid
                      console.log(res)
                      console.log("成功")
                      console.log(openid) //控制台打印openid
                      app.globalData.userOpenId = openid;
                      wx.setStorageSync('app.globalData.userOpenId', openid)
                    //   db.collection("user").then(e => {
                    //     console.log(e.total)
                    //     if (!e.total) {
                    //       user.add({
                    //         data: {
                    //           //每个用户的必要属性
                    //           userinfo
                    //           //openid会自带的
                    //         }
                    //       })
                    //     }
                    //   }).catch((error) => {
                    //     console.error();
                    // })
                    }          
                  })
                }
              })
          },
          fail() {
            wx.showToast({
              title: '请允许！',
              icon: 'none'
            })
          },
        })
      }
    }
    /*      if (e.currentTarget.dataset.status == '1') {
            wx.showModal({
              title: '温馨提示',
              content: '该功能还未开放',
          })
      
          return false
        }
      */
    wx.navigateTo({
      url: e.currentTarget.dataset.go
    })
  },
  //展示分享弹窗
  showShare() {
    this.setData({
      showShare: true
    });
  },
  //关闭弹窗
  closePop() {
    this.setData({
      showShare: false,
    });
  },
})